package com.example.night.service.MysqlService;

import com.example.night.data.InfluencerOperationRecord.InfluencerOperationRecord;
import com.example.night.data.InfluencerOperationRecord.InfluencerOperationRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class InfluencerOperationRecordMysqlService {
    @Autowired
    private InfluencerOperationRecordRepository repository;

    // 保存或更新操作记录
    public void saveOperation(InfluencerOperationRecord record) {
        Optional<InfluencerOperationRecord> existingRecord = repository.findByPromoteIdAndPhone(record.getPromoteId(), record.getPhone());

        if (existingRecord.isPresent()) {
            InfluencerOperationRecord updatedRecord = existingRecord.get();
            updatedRecord.setOperation(record.getOperation()); // 更新操作类型
            updatedRecord.setOperationTime(LocalDateTime.now()); // 更新操作时间
            updatedRecord.setWxId(record.getWxId()); // 更新微信号
            updatedRecord.setZfbId(record.getZfbId()); // 更新支付宝账号
            updatedRecord.setRoleType(record.getRoleType());
            repository.save(updatedRecord);
        } else {
            record.setOperationTime(LocalDateTime.now()); // 记录当前时间
            repository.save(record);
        }
    }

    // 查询所有操作记录
    public List<InfluencerOperationRecord> getAllRecords() {
        return repository.findAll();
    }

    // 查询所有被拒绝的记录（operation = -1）
    public List<InfluencerOperationRecord> getRejectedRecords() {
        return repository.findByOperation(-1);
    }

    public List<InfluencerOperationRecord> getNonRejectedRecords() {
        return repository.findByOperationNot(-1);
    }
}
